<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('应用列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="role-form">
                <div class="select-list">
                    <ul>
                        <li>
                            应用名称：<input type="text" name="appName"/>
                        </li>
                        <li>
                            应用状态：<select name="status" th:with="type=${@dict.getType('sys_normal_disable')}">
                            <option value="">所有</option>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                    th:value="${dict.dictValue}"></option>
                        </select>
                        </li>
                        <li class="select-time">
                            <label>创建时间： </label>
                            <input type="text" class="time-input" id="startTime" placeholder="开始时间"
                                   name="params[beginTime]"/>
                            <span>-</span>
                            <input type="text" class="time-input" id="endTime" placeholder="结束时间"
                                   name="params[endTime]"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()"
               :hasPermission="system:app:add">
                <i class="fa fa-plus"></i> 新增
            </a>
            <a class="btn btn-primary single disabled" onclick="$.operate.edit()"
               shiro:hasPermission="system:app:edit">
                <i class="fa fa-edit"></i> 修改
            </a>
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()"
               shiro:hasPermission="system:app:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table" data-page-size="10"
                   data-show-custom-view="true" data-custom-view="customViewFormatter"
                   data-show-custom-view-button="true">
            </table>
        </div>
    </div>
</div>

<template id="appTemplate">
    <div class="col-sm-3">
        <div class="contact-box">
            <h3><strong>%appName%</strong></h3>
            <hr/>
            <!--            <div class="text-center">-->
            <!--                <img alt="image" class="img-circle m-t-xs img-responsive" src="%IMAGE%">-->
            <!--                <div class="m-t-xs font-bold">%userName%</div>-->
            <!--            </div>-->
<!--            <p>应用地址： %appUrl%</p>-->
            <p>应用KEY： %clientId%</p>
            <p>应用密钥： %clientSecret%</p>
            <p>创建时间： %createTime%</p>
            <hr/>
            <div class="btn-group-sm" style="color: white; text-align: center">
                <a class="btn btn-primary" onclick="$.operate.edit('%appId%')"
                   shiro:hasPermission="system:app:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-success" onclick="intoApp('%appUrl%', '%clientId%')">
                    <i class="fa fa-plus"></i> 进入应用
                </a>
            </div>
            <div class="clearfix"></div>
        </div>
    </div>
</template>

<div th:include="include :: footer"></div>
<th:block th:include="include :: bootstrap-table-custom-view-js"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('system:app:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:app:remove')}]];

    var prefix = ctx + "system/app";
    var datas = [[${@dict.getType('sys_normal_disable')}]];

    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            modalName: "应用",
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            showExport: true,
            columns: [
                {
                    checkbox: true
                },
                {
                    field: 'appId',
                    title: '应用ID'
                },
                {
                    field: 'appName',
                    title: '应用名称'
                },
                {
                    visible: editFlag !== 'hidden',
                    title: '应用状态',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return statusTools(row);
                    }
                },
                {
                    field: 'appUrl',
                    title: '重定向地址'
                },
                {
                    field: 'clientId',
                    title: '应用KEY'
                },
                {
                    field: 'clientSecret',
                    title: '应用密钥'
                },
                {
                    field: 'sort',
                    title: '显示顺序',
                    sortable: true
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    sortable: true
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.roleId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.roleId + '\')"><i class="fa fa-remove"></i>删除</a> ');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    function customViewFormatter(data) {
        var template = $('#appTemplate').html()
        var view = ''
        $.each(data, function (i, row) {
            view += template.replace('%appName%', row.appName)
                .replaceAll('%appUrl%', row.appUrl)
                .replaceAll('%appId%', row.appId)
                .replaceAll('%clientId%', row.clientId)
                .replaceAll('%clientSecret%', row.clientSecret)
                .replaceAll('%createTime%', row.createTime)
        })

        return `<div class="row mx-0">${view}</div>`
    }

    /* 应用状态显示 */
    function statusTools(row) {
        if (row['status'] === 1) {
            return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="enable(\'' + row.appId + '\')"></i> ';
        } else {
            return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="disable(\'' + row.appId + '\')"></i> ';
        }
    }

    function disable(appId) {
        $.modal.confirm("确认要停用应用吗？", function () {
            $.operate.post(prefix + "/changeStatus", {"appId": appId, "status": 1});
        })
    }

    function enable(appId) {
        $.modal.confirm("确认要启用应用吗？", function () {
            $.operate.post(prefix + "/changeStatus", {"appId": appId, "status": 0});
        })
    }

    function intoApp(url, clientId) {
        debugger
        var encodeUrl = encodeURIComponent(url)
        window.parent.open("/passport/v1/public/auth2ssoLogin?appid=" + clientId + "&redirectUrl=" + encodeUrl
            + "&responseType=code", '_blank')
    }
</script>
</body>
</html>